// PeripheralInfo.cpp
#include "PeripheralInfo.h"
#include <soc/spi_pins.h>
#include <pins_arduino.h>

String PeripheralInfo::getAllPeripheralInfo()
{
    String info = "=== " + getChipModelName() + " Peripheral Information ===\n\n";

    info += getChipSpecificInfo() + "\n";

    if (hasADC())
    {
        info += "ADC (Analog to Digital Converter):\n" + getADCInfo() + "\n";
    }

    if (hasDAC())
    {
        info += "DAC (Digital to Analog Converter):\n" + getDACInfo() + "\n";
    }

    if (hasTouch())
    {
        info += "Touch Sensors:\n" + getTouchInfo() + "\n";
    }

    if (hasPWM())
    {
        info += "PWM (Pulse Width Modulation):\n" + getPWMInfo() + "\n";
    }

    if (hasSPI())
    {
        info += "SPI (Serial Peripheral Interface):\n" + getSPIInfo() + "\n";
    }

    if (hasI2C())
    {
        info += "I2C (Inter-Integrated Circuit):\n" + getI2CInfo() + "\n";
    }

    if (hasUART())
    {
        info += "UART (Universal Asynchronous Receiver/Transmitter):\n" + getUARTInfo() + "\n";
    }

    info += "Built-in LED:\n" + getLEDInfo() + "\n";
    info += "Buttons:\n" + getButtonInfo() + "\n";

    // 添加其他外设信息
    if (hasDedicatedGPIO())
    {
        info += "Dedicated GPIO:\n  Supported: Yes\n";
        info += "  Outward Channels: " + String(SOC_DEDIC_GPIO_OUT_CHANNELS_NUM) + " per core\n";
        info += "  Inward Channels: " + String(SOC_DEDIC_GPIO_IN_CHANNELS_NUM) + " per core\n\n";
    }

    if (hasTWAI())
    {
        info += "TWAI (Two-Wire Automotive Interface):\n  Supported: Yes\n";
        info += "  BRP Range: " + String(SOC_TWAI_BRP_MIN) + "-" + String(SOC_TWAI_BRP_MAX) + "\n\n";
    }

    if (hasUSBJTAG())
    {
        info += "USB Serial JTAG:\n  Supported: Yes\n\n";
    }

    if (hasTempSensor())
    {
        info += "Temperature Sensor:\n  Supported: Yes\n\n";
    }

    return info;
}

String PeripheralInfo::getChipSpecificInfo()
{
    String info = "Chip Information:\n";
    info += "  Model: " + getChipModelName() + "\n";
    info += "  Cores: " + String(SOC_CPU_CORES_NUM) + "\n";
    info += "  CPU Frequency: " + String(ESP.getCpuFreqMHz()) + " MHz\n";

    // 基于 soc_caps.h 的外设支持信息
    info += "  Peripherals: ";
    String peripherals = "";

    if (hasADC())
        peripherals += "ADC, ";
    if (hasDAC())
        peripherals += "DAC, ";
    if (hasTouch())
        peripherals += "Touch, ";
    if (hasPWM())
        peripherals += "PWM, ";
    if (hasSPI())
        peripherals += "SPI, ";
    if (hasI2C())
        peripherals += "I2C, ";
    if (hasUART())
        peripherals += "UART, ";
    if (hasDedicatedGPIO())
        peripherals += "DedicatedGPIO, ";
    if (hasTWAI())
        peripherals += "TWAI, ";
    if (hasUSBJTAG())
        peripherals += "USB-JTAG, ";
    if (hasTempSensor())
        peripherals += "TempSensor, ";

    if (peripherals.length() > 0)
    {
        peripherals = peripherals.substring(0, peripherals.length() - 2);
    }
    info += peripherals + "\n";

    // 内存信息
    info += "  Flash Size: " + String(ESP.getFlashChipSize() / (1024 * 1024)) + " MB\n";
    info += "  RAM Size: " + String(ESP.getHeapSize() / 1024) + " KB\n";
    info += "  PSRAM: " + String(ESP.getPsramSize() > 0 ? "Yes" : "No") + "\n";

    return info;
}

String PeripheralInfo::getADCInfo()
{
    String info = "";

#ifdef SOC_ADC_SUPPORTED
    info += "  Resolution: " + String(SOC_ADC_MAX_BITWIDTH) + "-bit\n";
    info += "  Voltage Range: 0-3.3V\n";
    info += "  ADC Units: " + String(SOC_ADC_PERIPH_NUM) + "\n";
    info += "  Max Channels: " + String(SOC_ADC_MAX_CHANNEL_NUM) + "\n";

#ifdef SOC_ADC_DIG_CTRL_SUPPORTED
    info += "  Digital Controller: Supported\n";
    info += "  Digital Channels: " + String(SOC_ADC_DIGI_CONTROLLER_NUM) + "\n";
    info += "  Pattern Length: " + String(SOC_ADC_PATT_LEN_MAX) + " items\n";
#endif

#ifdef SOC_ADC_ARBITER_SUPPORTED
    info += "  Arbiter: Supported\n";
#endif

#ifdef SOC_ADC_FILTER_SUPPORTED
    info += "  Filters: " + String(SOC_ADC_DIGI_FILTER_NUM) + "\n";
#endif

#ifdef SOC_ADC_MONITOR_SUPPORTED
    info += "  Monitors: " + String(SOC_ADC_DIGI_MONITOR_NUM) + "\n";
#endif

    // 根据实际芯片提供引脚信息
#if CONFIG_IDF_TARGET_ESP32
    info += "  ADC1 Channels: 0-7, 8-9 (typical)\n";
    info += "  ADC2 Channels: 0-9, 10-19 (typical)\n";
#elif CONFIG_IDF_TARGET_ESP32S2
    info += "  ADC1 Channels: 0-9\n";
    info += "  ADC2 Channels: 0-9\n";
#elif CONFIG_IDF_TARGET_ESP32C3
    info += "  ADC1 Channels: 0-5\n";
#endif

    info += "  Note: ADC2 cannot be used when WiFi is active (ESP32)\n";
#else
    info += "  Not supported on this chip\n";
#endif

    return info;
}

String PeripheralInfo::getDACInfo()
{
    String info = "";

#ifdef SOC_DAC_SUPPORTED
    info += "  Resolution: 8-bit (0-255)\n";
    info += "  Voltage Range: 0-3.3V\n";

    // 根据芯片型号提供准确的DAC信息
#if CONFIG_IDF_TARGET_ESP32
    info += "  Channels: GPIO 25, 26\n";
#elif CONFIG_IDF_TARGET_ESP32S2
    info += "  Channels: GPIO 17, 18\n";
#else
    info += "  Channels: Check datasheet\n";
#endif

    info += "  Max Output Current: 5mA\n";
#else
    info += "  Not supported on this chip\n";
#endif

    return info;
}

String PeripheralInfo::getTouchInfo()
{
    String info = "";

#ifdef SOC_TOUCH_SENSOR_NUM
    info += "  Sensitivity: Programmable\n";
    info += "  Wake-up capable: Yes\n";
    info += "  Touch Pads: " + String(SOC_TOUCH_SENSOR_NUM) + "\n";

    // 根据芯片型号提供触摸引脚信息
#if CONFIG_IDF_TARGET_ESP32
    info += "  Channels: GPIO 0, 2, 4, 12, 13, 14, 15, 27, 32, 33\n";
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
    info += "  Channels: Multiple GPIOs (check datasheet)\n";
#else
    info += "  Channels: Check datasheet\n";
#endif
#else
    info += "  Not supported on this chip\n";
#endif

    return info;
}

String PeripheralInfo::getPWMInfo()
{
    String info = "";

#ifdef SOC_LEDC_CHANNEL_NUM
    info += "  Channels: " + String(SOC_LEDC_CHANNEL_NUM) + " independent channels\n";
    info += "  Timer Bit Width: " + String(SOC_LEDC_TIMER_BIT_WIDE_NUM) + " bits\n";
    info += "  GPIO Support: Most GPIO pins\n";
    info += "  Frequency Range: 1Hz - 40MHz\n";

#ifdef SOC_LEDC_SUPPORT_XTAL_CLOCK
    info += "  XTAL Clock: Supported\n";
#endif
#else
    info += "  Not supported on this chip\n";
#endif

    return info;
}

String PeripheralInfo::getSPIInfo()
{
    String info = "";

#ifdef SOC_SPI_PERIPH_NUM
    info += "  SPI Controllers: " + String(SOC_SPI_PERIPH_NUM) + "\n";
    info += "  Max CS Pins: " + String(SOC_SPI_MAX_CS_NUM) + " per controller\n";
    info += "  Buffer Size: " + String(SOC_SPI_MAXIMUM_BUFFER_SIZE) + " bytes\n";

    // 提供默认SPI引脚

#ifdef MOSI
    info += "  Default SPI: MOSI=" + String(MOSI) +
            ", MISO=" + String(MISO) +
            ", SCK=" + String(SCK);
#ifdef SS
    info += ", SS=" + String(SS);
#endif
    info += "\n";
#endif

#ifdef SOC_SPI_SUPPORT_DDRCLK
    info += "  DDR Clock: Supported\n";
#endif

#ifdef SOC_SPI_SUPPORT_CONTINUOUS_TRANS
    info += "  Continuous Transfer: Supported\n";
#endif

    info += "  Max Speed: 80MHz\n";
    info += "  Note: Pins can be remapped to any GPIO\n";
#else
    info += "  Not supported on this chip\n";
#endif

    return info;
}

String PeripheralInfo::getI2CInfo()
{
    String info = "";

#ifdef SOC_I2C_NUM
    info += "  I2C Controllers: " + String(SOC_I2C_NUM) + "\n";
    info += "  FIFO Length: " + String(SOC_I2C_FIFO_LEN) + " bytes\n";

    // 默认I2C引脚
#ifdef SDA
    info += "  Default I2C: SDA=" + String(SDA) + ", SCL=" + String(SCL) + "\n";
#endif

#ifdef SOC_I2C_SUPPORT_HW_CLR_BUS
    info += "  Hardware Bus Clear: Supported\n";
#endif

#ifdef SOC_I2C_SUPPORT_XTAL
    info += "  XTAL Clock: Supported\n";
#endif

#ifdef SOC_I2C_SUPPORT_RTC
    info += "  RTC Clock: Supported\n";
#endif

    info += "  Max Speed: 1MHz (Fast Mode Plus)\n";
    info += "  Note: Pins can be remapped to any GPIO\n";
#else
    info += "  Not supported on this chip\n";
#endif

    return info;
}

String PeripheralInfo::getUARTInfo()
{
    String info = "";

#ifdef SOC_UART_NUM
    info += "  UART Controllers: " + String(SOC_UART_NUM) + "\n";
    info += "  FIFO Length: " + String(SOC_UART_FIFO_LEN) + " bytes\n";
    info += "  Max Baud Rate: " + String(SOC_UART_BITRATE_MAX) + " bps\n";

    // 默认UART引脚
#ifdef TX
    info += "  Default UART: TX=" + String(TX) + ", RX=" + String(RX) + "\n";
#endif

#ifdef SOC_UART_SUPPORT_RTC_CLK
    info += "  RTC Clock: Supported\n";
#endif

#ifdef SOC_UART_SUPPORT_XTAL_CLK
    info += "  XTAL Clock: Supported\n";
#endif

#ifdef SOC_UART_SUPPORT_WAKEUP_INT
    info += "  Wakeup Interrupt: Supported\n";
#endif

    info += "  Features: Hardware flow control, RS485, IrDA\n";
    info += "  Note: Pins can be remapped to any GPIO\n";
#else
    info += "  Not supported on this chip\n";
#endif

    return info;
}

// LED和Button信息保持不变
String PeripheralInfo::getLEDInfo()
{
    String info = "";

#ifdef LED_BUILTIN
    info += "  Built-in LED: GPIO " + String(LED_BUILTIN) + "\n";
#else
    info += "  No built-in LED defined\n";
#endif

    info += "  Note: Check your specific board schematic\n";

    return info;
}

String PeripheralInfo::getButtonInfo()
{
    String info = "";

#ifdef BUTTON_BUILTIN
    info += "  Built-in Button: GPIO " + String(BUTTON_BUILTIN) + "\n";
#endif

    info += "  Boot Button: GPIO 0 (typical for programming)\n";
    info += "  Note: GPIO 0 is typically used for boot mode selection\n";

    return info;
}

// 外设检测方法 - 基于 soc_caps.h 的实际定义
bool PeripheralInfo::hasADC()
{
#ifdef SOC_ADC_PERIPH_NUM
    return SOC_ADC_PERIPH_NUM > 0;
#else
    return false;
#endif
}

bool PeripheralInfo::hasDAC()
{
#ifdef SOC_DAC_SUPPORTED
    return SOC_DAC_SUPPORTED;
#else
    return false;
#endif
}

bool PeripheralInfo::hasTouch()
{
#ifdef SOC_TOUCH_SENSOR_NUM
    return (SOC_TOUCH_SENSOR_NUM > 0);
#else
    return false;
#endif
}

bool PeripheralInfo::hasPWM()
{
#ifdef SOC_LEDC_CHANNEL_NUM
    return SOC_LEDC_CHANNEL_NUM > 0;
#else
    return false;
#endif
}

bool PeripheralInfo::hasSPI()
{
#ifdef SOC_SPI_PERIPH_NUM
    return SOC_SPI_PERIPH_NUM > 0;
#else
    return false;
#endif
}

bool PeripheralInfo::hasI2C()
{
#ifdef SOC_I2C_NUM
    return SOC_I2C_NUM > 0;
#else
    return false;
#endif
}

bool PeripheralInfo::hasUART()
{
#ifdef SOC_UART_NUM
    return SOC_UART_NUM > 0;
#else
    return false;
#endif
}

bool PeripheralInfo::hasDedicatedGPIO()
{
#ifdef SOC_DEDICATED_GPIO_SUPPORTED
    return SOC_DEDICATED_GPIO_SUPPORTED;
#else
    return false;
#endif
}

bool PeripheralInfo::hasTWAI()
{
#ifdef SOC_TWAI_SUPPORTED
    return SOC_TWAI_SUPPORTED;
#else
    return false;
#endif
}

bool PeripheralInfo::hasUSBJTAG()
{
#ifdef SOC_USB_SERIAL_JTAG_SUPPORTED
    return SOC_USB_SERIAL_JTAG_SUPPORTED;
#else
    return false;
#endif
}

bool PeripheralInfo::hasTempSensor()
{
#ifdef SOC_TEMP_SENSOR_SUPPORTED
    return SOC_TEMP_SENSOR_SUPPORTED;
#else
    return false;
#endif
}

String PeripheralInfo::getChipModelName()
{
    const char *model = ESP.getChipModel();
    if (model)
    {
        return String(model);
    }

    // 备用方法检测芯片型号
#if CONFIG_IDF_TARGET_ESP32
    return "ESP32";
#elif CONFIG_IDF_TARGET_ESP32S2
    return "ESP32-S2";
#elif CONFIG_IDF_TARGET_ESP32S3
    return "ESP32-S3";
#elif CONFIG_IDF_TARGET_ESP32C3
    return "ESP32-C3";
#elif CONFIG_IDF_TARGET_ESP32C6
    return "ESP32-C6";
#elif CONFIG_IDF_TARGET_ESP32H2
    return "ESP32-H2";
#else
    return "Unknown ESP32";
#endif
}

String PeripheralInfo::formatPinList(const int *pins, int count)
{
    String result = "";
    for (int i = 0; i < count; i++)
    {
        result += String(pins[i]);
        if (i < count - 1)
        {
            result += ", ";
        }
    }
    return result;
}